package com.heng.ReadWriteLockDemo;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class LockDownGradingDemo3 {
    public static void main(String[] args) {
        ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

        ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
        ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();

        //本例同一线程
        writeLock.lock();
        System.out.println("---写入");
        writeLock.unlock();

        readLock.lock();
        System.out.println("---读取");
        readLock.unlock();


        writeLock.lock();
        System.out.println("---2次写入");
        readLock.lock();//写降级
        System.out.println("---2次读取");
        writeLock.unlock();
        readLock.unlock();

        readLock.lock();
        System.out.println("---读取");

        writeLock.lock();//读锁不能升级为读锁，在这里会卡住！
        System.out.println("---写入");
        writeLock.unlock();
        readLock.unlock();
    }
}